
package com.cy.model;

import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 13-9-9
* Time: 下午12:56
* To change this template use File | Settings | File Templates.
*/
@SuppressWarnings("serial")
@TableBind(tableName="service_message",pkName = "id")
public class ServiceMessage extends BasicModel<ServiceMessage> {
    public static final ServiceMessage dao = new ServiceMessage();
    public static final String SELECT_ALL = "`id`,`status`,`user_id`,`message`,`type`,`create_time`,`account_id`,`reply`,`reply_time` ";

    @Override
    public Page<ServiceMessage> paginate(Integer pageNumber, Integer pageSize, String column, ServiceMessage log) {
        List<Object> paras = new ArrayList<>();
        StringBuffer sqlExceptSelectBuffer = new StringBuffer("FROM " + getTableName());
        sqlExceptSelectBuffer.append(" sm ,user u ");
        sqlExceptSelectBuffer.append(" where u.user_id =  sm.user_id ");
        StringBuffer selectBuffer = new StringBuffer("SELECT ").append(column);
        if (pageNumber == null || pageNumber == 0) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }

        if (log != null) {
            String[] attrNames = log.getAttrNames();
            for (String attrName : attrNames) {
                Object attrValue = log.get(attrName);
                if (attrValue != null) {

                    if("start".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `create_time` > DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("end".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `create_time` < DATE_FORMAT(?,'%Y-%m-%d %T')");
                    }else if("message".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND `message` like ?");
                        attrValue = "%"+attrValue+"%";
                    }else if("mobile".equals(attrName)){
                        sqlExceptSelectBuffer.append(" AND u.`mobile` = ?");
                    }else{
                       /* int index = attrName.indexOf(".");
                        if (index > 0){
                            sqlExceptSelectBuffer.append(" AND `"+attrName.substring(0,index)+"`.`" + attrName.substring(index+1) + "`=?");
                        }else {*/
                        sqlExceptSelectBuffer.append(" AND sm.`" + attrName + "`=?");
//                        }
                    }
                    paras.add(attrValue);
                }
            }
        }

        if (paras.size() > 0) {
            return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString(), paras.toArray());
        }
        return paginate(pageNumber, pageSize, selectBuffer.toString(), sqlExceptSelectBuffer.toString());
    }

    public List<ServiceMessage> findUserMsg(Long userId){
        return find("select * from "+this.getTableName()+" where user_id = ?",userId);
    }
    
    public ServiceMessage setId (Long id){
        this.set("id",id);
        return this;
    }

    public Long getId (){
        return this. get("id");
    }

        public ServiceMessage setStatus (Integer status){
        this.set("status",status);
        return this;
    }

    public Integer getStatus (){
        return this. get("status");
    }
        public ServiceMessage setIsRead (Integer isRead){
        this.set("is_read",isRead);
        return this;
    }

    public Integer getIsRead (){
        return this. get("is_read");
    }

        public ServiceMessage setUserId (Long userId){
        this.set("user_id",userId);
        return this;
    }

    public Long getUserId (){
        return this. get("user_id");
    }

        public ServiceMessage setMessage (String message){
        this.set("message",message);
        return this;
    }

    public String getMessage (){
        return this.getStr("message");
    }

        public ServiceMessage setType (Integer type){
        this.set("type",type);
        return this;
    }

    public Integer getType (){
        return this. get("type");
    }

        public ServiceMessage setCreateTime (Date createTime){
        this.set("create_time",createTime);
        return this;
    }

    public Date getCreateTime (){
        return this.getDate("create_time");
    }

        public ServiceMessage setAccountId (Long accountId){
        this.set("account_id",accountId);
        return this;
    }

    public Long getAccountId (){
        return this. get("account_id");
    }

        public ServiceMessage setReply (String reply){
        this.set("reply",reply);
        return this;
    }

    public String getReply (){
        return this.getStr("reply");
    }

        public ServiceMessage setReplyTime (Date replyTime){
        this.set("reply_time",replyTime);
        return this;
    }

    public Date getReplyTime (){
        return this.getDate("reply_time");
    }
}
